package com.code.gen.util;

import java.util.List;

import com.code.gen.bean.bo.CodeGenProperties;
import com.code.gen.bean.bo.CodeGenPropertiesSystem;
import com.sojson.util.StringUtil;

import lombok.Data;

/**
 * 初始化程序静态数据类
 * 
 * @author liu
 * @date 2020-08-20
 */
@Data
public class CodeGenInitStaticFieldUtil {

    private CodeGenProperties properties;

    public CodeGenInitStaticFieldUtil(CodeGenProperties properties) {
        this.properties = properties;
    }

    /**
     * 初始化方法
     */
    public void init() {
        CodeGenPropertiesSystem system = getProperties().getSystem();

        initApplicationProperties(system);
    }

    /**
     * 初始化启动类属性
     * 
     * @param system
     */
    private void initApplicationProperties(CodeGenPropertiesSystem system) {
        system.setId(StringUtil.toUnderScoreCase(system.getId()).toLowerCase());
        system.setSerialNumber(StringUtil.toUnderScoreCase(system.getSerialNumber()).toLowerCase());
        system.setDelFlag(StringUtil.toUnderScoreCase(system.getDelFlag()).toLowerCase());
        system.setDatabaseTablePrefix(system.getDatabaseTablePrefix().toLowerCase());

        List<String> sort = system.getSort();
        int sortSize = sort.size();
        for (int i = 0; i < sortSize; i++) {
            sort.set(i, StringUtil.toUnderScoreCase(sort.get(i)).toUpperCase());
        }
        List<String> noFields = system.getNoFields();
        int noFieldsSize = noFields.size();
        for (int i = 0; i < noFieldsSize; i++) {
            noFields.set(i, StringUtil.toUnderScoreCase(noFields.get(i)).toUpperCase());
        }
        List<String> queryFields = system.getQueryFields();
        int queryFieldsSize = queryFields.size();
        for (int i = 0; i < queryFieldsSize; i++) {
            queryFields.set(i, StringUtil.toUnderScoreCase(queryFields.get(i)).toUpperCase());
        }
        List<String> queryNoFields = system.getQueryNoFields();
        int queryNoFieldsSize = queryNoFields.size();
        for (int i = 0; i < queryNoFieldsSize; i++) {
            queryNoFields.set(i, StringUtil.toUnderScoreCase(queryNoFields.get(i)).toUpperCase());
        }
        List<String> resultFields = system.getResultFields();
        int resultFieldsSize = resultFields.size();
        for (int i = 0; i < resultFieldsSize; i++) {
            resultFields.set(i, StringUtil.toUnderScoreCase(resultFields.get(i)).toUpperCase());
        }
        List<String> resultNoFields = system.getResultNoFields();
        int resultNoFieldsSize = resultNoFields.size();
        for (int i = 0; i < resultNoFieldsSize; i++) {
            resultNoFields.set(i, StringUtil.toUnderScoreCase(resultNoFields.get(i)).toUpperCase());
        }

        // 初始化基础路径
        String basePath = system.getBasePath();
        if (basePath != null) {
            basePath = CodeGenReadTemplate.removeD(basePath.replace(" ", "").replace("\\", ".").replace("/", "."));
            if (basePath.length() == 0) {
                basePath = "";
            }
        } else {
            basePath = "";
        }
        system.setBasePath(basePath);
        String moduleName = system.getModuleName();
        if (moduleName != null) {
            moduleName = CodeGenReadTemplate.removeD(moduleName.replace(" ", "").replace("\\", ".").replace("/", "."));
            if (moduleName.length() == 0) {
                moduleName = "";
            }
        } else {
            moduleName = "";
        }
        String businessName = system.getBusinessName();
        if (businessName != null) {
            businessName =
                CodeGenReadTemplate.removeD(businessName.replace(" ", "").replace("\\", ".").replace("/", "."));
            if (businessName.length() == 0) {
                businessName = "";
            }
        } else {
            businessName = "";
        }
        if (StringUtil.isBlank(moduleName)) {
            moduleName = businessName;
        } else {
            moduleName = moduleName + "." + businessName;
        }
        system.setModuleName(moduleName);
    }

}